@extends('admin::layouts.master')
@section('title')
    添加礼包
@endsection
@section('tab')
		<div class="layui-fluid">
            <div class="layui-row">
                <form class="layui-form" >
                	{{csrf_field()}}
                    {{hi_form_hidden('id',$info->id)}}
                    <div class="layui-form-item">
                        <label for="username" class="layui-form-label">
                            <span class="x-red">*</span>权益名称</label>
                        <div class="layui-input-inline" style="width: 300px;">
                            <input type="text" value="{{$info->name}}" autocomplete="off" class="layui-input" disabled=""></div>
                    </div>
                    <div class="layui-form-item">
                        <label for="username" class="layui-form-label">
                            <span class="x-red">*</span>备注、说明</label>
                        <div class="layui-input-inline" style="width: 300px;">
                            <textarea placeholder="请输入内容" id="desc" class="layui-textarea" disabled="">{{$info->remark}}</textarea>
                        </div>
                    </div>
                    @foreach($arr_list as $k=>$val)
                        <div class="layui-form-item x-city" id="gitfs_{{$val['id']}}">
                            <label class="layui-form-label"><span class="x-red">*</span>赠送券种</label>
                            <div class="layui-input-inline">
                                <select id="coupon_{{$val['id']}}"  lay-filter="province">
                                    <option value="">请选择</option>
                                    @foreach($coupon_list as $key=>$value)
                                        <option value="{{$value['id']}}" @if($val['coupon_id']== $value['id'] ) selected @endif >{{$value['name']}}</option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="layui-input-inline">
                                <select id="type_{{$val['id']}}"  lay-filter="province">
                                    <option value="">请选择赠送方式</option>
                                    <option value="1" @if($val['type']== 1 ) selected @endif >一次性赠送</option>
                                    <option value="2" @if($val['type']== 2 ) selected @endif >每月赠送</option>
                                </select>
                            </div>
                            <div class="layui-input-inline">
                                <select id="num_{{$val['id']}}" lay-filter="province">
                                    <option value="">请选择赠送张数</option>
                                    @for( $i = 1 ; $i<=5 ; $i++ )
                                        <option value="{{$i}}" @if($val['num']== $i ) selected @endif >{{$i}}</option>
                                    @endfor
                                </select>
                            </div>
                             <button class="layui-btn" lay-filter="but_edit" data-id="{{$val['id']}}" lay-submit="">编辑</button>
                             <button class="layui-btn" lay-filter="but_del" data-id="{{$val['id']}}"  lay-submit="">删除</button>
                        </div>
                    @endforeach

                    <div class="layui-form-item x-city" id="start">
                        <label class="layui-form-label"><span class="x-red">*</span>赠送券种</label>
                        <div class="layui-input-inline">
                            <select name="coupon_id" lay-filter="province">
                                <option value="">请选择</option>
                                @foreach($coupon_list as $key=>$value)
                                    <option value="{{$value['id']}}">{{$value['name']}}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="layui-input-inline">
                            <select name="type" lay-filter="province">
                                <option value="">请选择赠送方式</option>
                                <option value="1">一次性赠送</option>
                                <option value="2">每月赠送</option>
                            </select>
                        </div>
                        <div class="layui-input-inline">
                            <select name="num" lay-filter="province">
                                <option value="">请选择赠送张数</option>
                                @for( $i = 1 ; $i<=5 ; $i++ )
                                    <option value="{{$i}}">{{$i}}</option>
                                @endfor
                            </select>
                        </div>
                    </div>




			        <div class="layui-form-item">
			            <label for="L_repass" class="layui-form-label"></label>
			            <button class="layui-btn" lay-filter="but_save" lay-submit="">确认</button>
			        </div>
        		</form>
        	</div>
        </div>

@endsection

@section('tab_js')
    <script>

        layui.use(['form', 'layer', 'jquery'], function () {
            $ = layui.jquery;
            var form = layui.form,
            layer = layui.layer;
            //监听提交
            var is_submit = false;
            form.on('submit(but_save)', function (data) {
                if( is_submit ){
                    return false;
                }
                is_submit = true;
                $.ajax({
                    url: js_url('admin-grade-store_gifts'),
                    type: 'post',
                    data: data.field,
                    dataType: "json",
                    success: function (result) {
                        if (result.state === 0) {
                            layer.msg("增加成功", {time: 500, icon: 6}, function () {
                                //关闭当前frame
                                xadmin.close();
                                // 可以对父窗口进行刷新
                                xadmin.father_reload();
                            });
                        } else {
                            layer.msg(result.msg, {time: 500, icon: 5});
                        }
                    }
                });
                return false;

            });


            form.on('submit(but_del)', function (data) {
                var that = $(this);
                var id = that.attr('data-id');
                $.ajax({
                    url: js_url('admin-grade-del_gifts'),
                    type: 'delete',
                    data: {id:id},
                    dataType: "json",
                    success: function (result) {
                        if (result.state === 0) {
                            layer.msg("删除成功", {time: 1000, icon: 6}, function () {
                                $("#gitfs_"+id).remove();
                                return false;
                            });
                        } else {
                            layer.msg(result.msg, {time: 1000, icon: 5});
                        }
                    }
                });
                return false;

            });




            form.on('submit(but_edit)', function (data) {
                var that = $(this);
                var id = that.attr('data-id');
                layer.confirm("确认要修改吗", function(index){
                    var coupon_id = $("#coupon_"+id).val();
                    var type = $("#type_"+id).val();
                    var num = $("#num_"+id).val();
                    var data = {id:id,coupon_id:coupon_id,type:type,num:num,'_token': '{{csrf_token()}}'};
                    $.ajax({
                        url: js_url('admin-grade-update_gifts'),
                        type: 'post',
                        data: data,
                        dataType: "json",
                        success: function (result) {
                            if (result.state === 0) {
                                layer.msg("修改成功", {time: 1000, icon: 6}, function () {
                                    
                                });
                            } else {
                                layer.msg(result.msg, {time: 1000, icon: 5});
                            }
                        }
                    });
                });
                return false;
            });
        });
    </script>
@endsection